//
//  MineViewController.m
//  AroundTheWorld
//
//  Created by IfelseGo on 16/5/14.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "MineViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "MineModel.h"
#import "MineTableViewCell.h"
#import "MineLoginVC.h"
#import "mineLoginMailVC.h"
#import "MineSetVC.h"
#import "MineFavoriteVC.h"

static NSString *const mineReuse = @"mineReuse";

@interface MineViewController ()
@property (nonatomic, strong) UITableView *myTabelView;
@property (nonatomic, strong) UIImageView *backImageV;
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSString *userPic;
@end

@implementation MineViewController

-(void)viewWillAppear:(BOOL)animated{
    NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
    self.userName = [data objectForKey:@"userName"];
    self.userPic = [data objectForKey:@"userPic"];
    [self.myTabelView reloadData];
    if (self.userName  == nil) {
//        [self addHeader];
    }else{
       self.loginLabel.text = self.userName;
       [self.loginImageV sd_setImageWithURL:[NSURL URLWithString:self.userPic] placeholderImage:[UIImage imageNamed:@""]];
     }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self getData];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:mineReuse];
    
    //添加头视图
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 350)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.tableHeaderView = headerView;
    [self addHeader];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0xCFCFCF);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0x3AA082,0xCFCFCF);

    
    //添加夜间模式按钮
    UIButton *naight = [UIButton buttonWithType:UIButtonTypeCustom];
    [naight setImage:[UIImage imageNamed:@"btn_sun32"] forState:UIControlStateNormal];
    naight.frame = CGRectMake(25, 25, 30, 30);
    [self.myTabelView addSubview:naight];
    [naight addTarget:self action:@selector(buttonAtcion:) forControlEvents:UIControlEventTouchUpInside];

}

//夜间模式判断
- (void)buttonAtcion:(UIButton *)item{
//    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
        if (item.selected == YES) {
            [item setImage:[UIImage imageNamed:@"btn_sun32"] forState:UIControlStateNormal];
//        item.image = [[UIImage imageNamed:@"btn_sun32"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        NSLog(@"白天");
        [self.dk_manager dawnComing];
    } else {
        NSLog(@"晚上");
        [item setImage:[UIImage imageNamed:@"btn_sun_night"] forState:UIControlStateNormal];
//        item.image = [[UIImage imageNamed:@"btn_sun_night"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.dk_manager nightFalling];
    }
    item.selected = !item.selected;
}

//数据
- (void)getData
{
    self.myArrayM = [NSMutableArray array];
    self.myArrayMA = [NSMutableArray array];
    self.myArrayMB = [NSMutableArray array];
    self.myArrayMC = [NSMutableArray array];
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"我的订单", @"我收藏的折扣", @"我的优惠劵",@"我收藏的目的地", @"我的足迹", @"等我点评的目的地",@"我发布的帖子", @"我的问答", @"我的结伴", @"我的论坛组", nil];
    for (int i = 0; i < 10; i++) {
        MineModel *model = [[MineModel alloc] init];
        model.title = [array objectAtIndex:i];
        model.photo = [NSString  stringWithFormat:@"btn_mine_icon%d",i+1];
        if (i < 3) {
            [self.myArrayMA addObject:model];
        }else if (i >= 3 && i < 6) {
            [self.myArrayMB addObject:model];
        }else {
           [self.myArrayMC addObject:model];
        }
    }
    [self.myArrayM addObject:self.myArrayMA];
    [self.myArrayM addObject:self.myArrayMB];
    [self.myArrayM addObject:self.myArrayMC];
//    NSLog(@"%@", self.myArrayM);
}
//addHearder
- (void)addHeader
{
    //底图
    self.backImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profileHeader"]];
    self.backImageV.frame = CGRectMake(0, 0, WIDTH, 350);
    [self.myTabelView addSubview:self.backImageV];
    
    UIImageView *imagev = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"GroupMember_Cell_Mask-1"]];
    imagev.alpha = 0.3;
    imagev.frame = CGRectMake(0, 0, WIDTH, 350);
    [self.myTabelView addSubview:imagev];
    
    //白色透明条
    UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(0, 240, WIDTH, 110)];
    backV.backgroundColor = [UIColor clearColor];
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 240, WIDTH, 110)];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    toolbar.alpha = 0.3;
    [self.myTabelView addSubview:toolbar];

    //loginButton
    [self addHeaderLoginButton];
    
    //添加按钮
    [self addButton:@"收藏的帖子" Number:1 imageName:@"btn_mine_btn1"];
    [self addButton:@"我的锦囊" Number:2 imageName:@"btn_mine_btn2"];
    [self addButton:@"我的行程" Number:3 imageName:@"btn_mine_btn3"];
}

- (void) addHeaderLoginButton
{
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"btn_mine_login_user"]];
    image.backgroundColor = [UIColor whiteColor];
    image.layer.masksToBounds = YES;
    image.layer.cornerRadius = 35;
    image.frame = CGRectMake(30, 135, 70, 70);
    [self.myTabelView addSubview:image];
    self.loginImageV = image;
    
    
    UIButton *buttonSet = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonSet setImage:[UIImage imageNamed:@"btn_mine_header_set"] forState:UIControlStateNormal];
    buttonSet.layer.masksToBounds = YES;
    buttonSet.layer.cornerRadius = 15;
    buttonSet.frame = CGRectMake(WIDTH - 50, 10, 44, 44);
    [self.myTabelView addSubview:buttonSet];
    [buttonSet addTarget:self action:@selector(buttonSetAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.loginLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 135, WIDTH / 2, 40)];
    self.loginLabel.backgroundColor = [UIColor clearColor];
    self.loginLabel.textColor = [UIColor whiteColor];
    self.loginLabel.font = [UIFont systemFontOfSize:22];
    self.loginLabel.text = @"点击登录, 体验更多";
    [self.myTabelView addSubview:self.loginLabel];
    
    UILabel *fansLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 170, WIDTH / 2, 40)];
    fansLabel.backgroundColor = [UIColor clearColor];
    fansLabel.text = [NSString stringWithFormat:@"粉丝 %ld   关注 %ld", self.fansNumber, self.followNumber];
    fansLabel.textColor = [UIColor whiteColor];
    [self.myTabelView addSubview:fansLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    button.frame = CGRectMake(0, 135, WIDTH, 100);
    [self.myTabelView addSubview:button];
    [button addTarget:self action:@selector(loginButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)addButton:(NSString *)labelName Number:(NSInteger )number imageName:(NSString *)imageName{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0 + WIDTH / 3 * (number - 1), 305, WIDTH / 3, 25)];
    label.backgroundColor = [UIColor clearColor];
    label.text = labelName;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = [UIColor whiteColor];
    [self.myTabelView addSubview:label];
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(55 + 140 * (number - 1) , 265, 30, 35)];
    imageV.image = [UIImage imageNamed:imageName];
    [self.myTabelView addSubview:imageV];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    button.frame = CGRectMake(0 +  WIDTH / 3 * (number - 1), 240, WIDTH / 3 , 110);
    button.tag = 300000 + number;
    [button addTarget:self action:@selector(buttonAc) forControlEvents:UIControlEventTouchUpInside];
}
//设置按钮
- (void)buttonSetAction:(UIButton *)button
{
    MineSetVC *setVC = [[MineSetVC alloc] init];
    [self presentViewController:setVC animated:YES completion:^{
        
        
    }];
}

- (void)buttonAc
{
    
}


- (void)loginButtonAction:(UIButton *)button
{
    if (self.userName  == nil) {
        mineLoginMailVC *loginVC = [[mineLoginMailVC alloc] initWithNibName:@"mineLoginMailVC" bundle:nil];
        [self presentViewController:loginVC animated:YES completion:^{
            
        }];
    }else
    {
         NSUserDefaults *deta = [NSUserDefaults standardUserDefaults];
        [self showAlert:@"用户成功登出!"];
//         [self addAlertControllerWithTitle:@"确定登出账号?" withMessage:@""];
         [deta removeObjectForKey:@"userName"];
         [deta removeObjectForKey:@"userPic"];
         self.loginLabel.text = @"点击登录, 体验更多";
        self.loginImageV.image = [UIImage imageNamed:@"btn_mine_login_user"];
//         [self.loginImageV sd_setImageWithURL:[NSURL URLWithString:@"btn_mine_login_user"] placeholderImage:[UIImage imageNamed:@""]];
         [self getData];
        
    }
}


//分区的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.myArrayM.count;
}

//分区头高度
-(CGFloat )tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //背景颜色
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:235 / 255.0 blue:235 / 255.0 alpha:1.0];
    return view;
}

//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.myArrayMA.count;
    }
    if (section == 1) {
        return  self.myArrayMB.count;
    }
    return self.myArrayMC.count;
}

//建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mineReuse];
     if (indexPath.section == 0) {
        MineModel *model = [self.myArrayMA objectAtIndex:indexPath.row];
         cell.myModel = model;
          cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.section == 1) {
        MineModel *model = [self.myArrayMB objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    MineModel *model = [self.myArrayMC objectAtIndex:indexPath.row];
    cell.myModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSUserDefaults *deta = [NSUserDefaults standardUserDefaults];
    if (self.userName  == nil) {
        mineLoginMailVC *loginMailVC = [[mineLoginMailVC alloc] init];
        [self presentViewController:loginMailVC animated:YES completion:^{
            
        }];
        
    } else{
        if (indexPath.section == 0) {
            if (indexPath.row == 1) {
                MineFavoriteVC *favoriteVC = [[MineFavoriteVC alloc] init];
                [self presentViewController:favoriteVC animated:YES completion:^{
                    
                    
                }];
            }
        }
        
    }
    
}



//刷新后显示对话框
- (void)timerFireMethod:(NSTimer*)theTimer  //弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}

- (void)showAlert:(NSString *) _message{   //时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:1.5f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}


//创建

-(void)addAlertControllerWithTitle:(NSString *)acTitle withMessage:(NSString *)message
{
    //取消按钮
    
    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:acTitle message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertC addAction:cancelAction];
    
    UIAlertAction *defaltAction = [UIAlertAction actionWithTitle:@"登出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self.myTabelView reloadData];
    }];
    
    [alertC addAction:defaltAction];
    
    //运行
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
